<?php
    /* Include the Player Class */
    $path = $_SERVER['DOCUMENT_ROOT'];
    $db_path = $path."/fnt-lib/_class_player.php";
    include_once($db_path);

    $path = $_SERVER['DOCUMENT_ROOT'];
   $db_path = $path."/lib/database.php";
   include_once($db_path);
	
class _game{
	
	private $gid;
    private $owner;
    private $title;
    private $desc;
    private $type;
	private $rules;
	
	private $status;
	

	/* VARIABLES FOR THE CLASS */
	/* The following is the number of Rules  (default = 3)*/
	private static $_NUM_RULES = 3;
	/* Option of having a type associated with the rule */
	private static $_OPTION_RULE_TYPE = false;
	/* Option of having a type associated with the game */
	private static $_OPTION_TYPE = false;
	/* Option of having the players add the game */
	private static $_OPTION_ADD = true;
	/* Option of having the players vote on the game's status */
	private static $_OPTION_VOTE = false;
	/* Percent of votes needed to accept the game */
	private static $_ACCEPT_PCNT = .50;

	/* Accessors and Changers for the class variables 
	
		Available types: Add,Type,Rule, and Vote
	
	*/
	public static function get_opt($player,$type){
		if($player->is_admin()){
			switch(strtolower($type)){
				case 'add':
					return _game::$_OPTION_ADD;	
				case 'type':
					return _game::$_OPTION_TYPE;
				case 'rule':
				case 'rule type':
				case 'rule_type':
					return _game::$_OPTION_RULE_TYPE;
				case 'vote':
					return _game::$_OPTION_VOTE;
			}
		}
		return false;
	}
	public static function tog_opt($player,$type){
		if($player->is_admin()){
			switch(strtolower($type)){
				case 'add':
					 _game::$_OPTION_ADD = !(_game::$_OPTION_ADD);	
					 return true;
				case 'type':
					 _game::$_OPTION_TYPE = !(_game::$_OPTION_TYPE);	
					 return true;
				case 'rule':
				case 'rule type':
				case 'rule_type':
					 _game::$_OPTION_RULE_TYPE = !(_game::$_OPTION_RULE_TYPE);	
					 return true;
				case 'vote':
					 _game::$_OPTION_VOTE = !(_game::$_OPTION_VOTE);	
					 return true;
			}
		}
		return false;
	}
	
	/* Utilities */
	public function _game($game_id){ //Load
		$loaded = false;

		/**  Add the functions to the Action list **/




		/* Load up the variables */
		dblogin();
		$this->gid = $game_id;
		$q = "select game, des, owner, type from GAMES where gameid = ".$this->gid;
		$r = mysql_query($q);
		echo $q,'<br>';
		if ($r !== false && mysql_num_rows($r) > 0 ) {
			while ( $a = mysql_fetch_assoc($r) ) {
				$this->title = stripslashes($a['game']);
				$this->desc =  stripslashes($a['des']);
				$this->type =  stripslashes($a['type']);
				$this->owner = stripslashes($a['owner']);

				/*If all have been loaded*/
				$loaded = true;		
			}
		}

		/* If there was an error, it will show up as false */
		return $loaded;
	}
	
	public function write($post){ //Save
		dblogin();

		/* Gather the fields from the $p array */
		if($p['title'])
			$title = mysql_real_escape_string($p['title']);
		if($p['desc'])
			$desc = mysql_real_escape_string($p['desc']);
		if($p['type'])
			$type = mysql_real_escape_string($p['type']);

		/*  Number of rules is dynamic, allows for greater customization */
		for($i=1;$i <= $_NUM_RULES; $i++){
			if($p['rules'.$i.'title'])
				$rules[$i]['title'] = mysql_real_escape_string($p['rules'.$i.'title']);
			if($p['rules'.$i.'type'])
				$rules[$i]['type'] = mysql_real_escape_string($p['rules'.$i.'type']);
			if($p['rules'.$i.'desc'])
				$rules[$i]['desc'] = mysql_real_escape_string($p['rules'.$i.'desc']);
		}
		
		/* Test for each neccessary field s*/
		if($title && $desc && $type){
			/* Add it to the database!*/
			$sql = "insert into GAMES values ('$title','$desc','$add_dt'".write_plugin($p).")";
			return mysql_query($sql);
		}else{
			/* Do nothing */
			return false;
		}		
	}
	public function edit($post){ //Overwrite the save with the new information
	}
	public function display(){//Print
		echo 'Title: ',$this->title,'<br>';
		echo 'Desc: ',$this->desc,'<br>';
		echo 'Owner: ',$this->owner,'<br>';
	}
	public function form_add($player){//Create the form to add to DB
		if($player->is_admin() || _game::$_OPTION_ADD){
			$output = <<<ADMIN_FORM

			<form action="{$_SERVER['PHP_SELF']}" method="post">
			  <label for="title">Title:</label>
			  <input name="title" id="title" type="text" maxlength="150" />
			  <label for="bodytext">Body Text:</label>
			  <textarea name="bodytext" id="bodytext"></textarea>
ADMIN_FORM;
			$output .= '<input type="submit" value="Create This Entry!" /></form>';
			}
		
		return $output;
	}
	
	
	
	public function form_edit($player){//Create the form to add to DB
		if($player->is_admin() || $this->gid = $player->get_nbr()){
			echo 'Allow Edit<br>';	
		}
	
	}

	
	/* Functionality */
	public function vote($player, $vote){//Ability to change the status of the game 
		/* Check for  allowances for  voting*/
		if($player->is_admin() || _game::$_OPTION_VOTE){
			$this->update_vote($player);
			
			//Gather the vote totals
			$q = "";
			
			$pos_vote = 0;
			$idc_vote = 0;
			
			//given the amounts update_status
			//$this->update_status($pos_vote,$idc_vote);
			
		}
	}
	
	/* Helper functions */
	private function update_status($pos_vote,$idc_vote){//modify the game's status as needed
		if($status != 'D'){
			//Account is still active, change status
			if(($pos_vote)/($_FRAME->active_players() - $idc_vote) > $_FRAME->$_ACCEPT_PCNT){
				//Change status to POS
			}else{	
				//Change status to Neg
			}
		}
	}
	private function update_vote($player){//save the player's vote
	}
}

?>